// #include "../source/Socket.hpp"
// #include "../source/Channel.hpp"
// #include "../source/Poller.hpp"
// #include "../source/EventLoop.hpp"

// void closecallback(Channel *channel)
// {
// 	DEL_LOG("close:%d",channel->Fd());
// 	channel->Remove();
// 	delete channel;
// }
// void readcallback(Channel *channel)
// {
// 	char buff[1024] = {0};
// 	int ret = ::recv(channel->Fd(), buff, sizeof(buff), 0);
// 	if (ret < 0)
// 	{
// 		closecallback(channel);
// 		return;
// 	}
// 	DEL_LOG("%s",buff);
// 	channel->EnableWriteableEvent();
// }
// void WriteCallback(Channel *channel)
// {
// 	const char *buf = "你好";
// 	int ret = ::send(channel->Fd(), buf, sizeof(buf), 0);
// 	if (ret < 0)
// 	{
// 		closecallback(channel);
// 		return;
// 	}
// 	channel->CloseWriteableEvent();
// }
// void ErrorCallback(Channel *channel)
// {
// 	closecallback(channel);
// }

// void eventCallback(EventLoop *p,Channel *channel,int timerfd)
// {
// 	p->RefreshTask(timerfd);
// }
// void Accept(EventLoop *p,Channel *channel)
// {
// 	int newfd = accept(channel->Fd(), nullptr, nullptr);
// 	if (newfd < 0)
// 	{
// 		return;
// 	}
// 	Channel *ch = new Channel(p, newfd);
// 	int timerfd = rand() % 1000;
// 	ch->SetReadCallback(std::bind(readcallback,ch));
// 	ch->SetWriteCallback(std::bind(WriteCallback,ch));
// 	ch->SetCloseCallback(std::bind(closecallback,ch));
// 	ch->SetErrorCallback(std::bind(ErrorCallback,ch));
// 	ch->SetEventCallback(std::bind(eventCallback,p,ch,timerfd));
// 	p->TaskAdd(timerfd,10,std::bind(closecallback,ch));
// 	ch->EnableReadableEvent();
// }

// int main()
// {
// 	srand(time(NULL));
// 	EventLoop loop;
// 	Socket s;
// 	s.CreateTcpServer(8080);
// 	Channel *channel = new Channel(&loop, s.Fd());
// 	channel->SetReadCallback(std::bind(Accept,&loop,channel));
// 	channel->EnableReadableEvent();
// 	while (true)
// 	{
// 		sleep(1);
// 		loop.Start();
// 	}
// 	s.Close();
// 	return 0;
// }

// #include "../source/Socket.hpp"
// #include "../source/Channel.hpp"
// #include "../source/Poller.hpp"
// #include "../source/EventLoop.hpp"
// #include "../source/Connection.hpp"

// std::unordered_map<int,Ptrconnection> _conn;
// int id = 0;
// void closecallback(Channel *channel)
// {
// 	DEL_LOG("close:%d",channel->Fd());
// 	channel->Remove();
// 	delete channel;
// }
// void readcallback(Channel *channel)
// {
// 	char buff[1024] = {0};
// 	int ret = ::recv(channel->Fd(), buff, sizeof(buff), 0);
// 	if (ret < 0)
// 	{
// 		closecallback(channel);
// 		return;
// 	}
// 	DEL_LOG("%s",buff);
// 	channel->EnableWriteableEvent();
// }
// void OnConnextDis(const Ptrconnection &con)
// {
// 	_conn.erase(con->Id());
// }
// void OnConnect(const Ptrconnection &con)
// {
// 	DEL_LOG("new connection : %p",con->GetContext());
// }

// void OnMessage(const Ptrconnection &con,Buffer *buff)
// {
// 	DEL_LOG("%s",buff->ReadPosition());
// 	buff->MoveReadOffset(buff->ReadableSize());
// 	std::string str = "hell bit";
// 	con->Send(str.c_str(),str.size());
// 	con->Shutdown();
// }

// void Accept(EventLoop *p,Channel *channel)
// {
// 	int newfd = accept(channel->Fd(), nullptr, nullptr);
// 	if (newfd < 0)
// 	{
// 		return;
// 	}
// 	id++;
// 	Ptrconnection ch(new Connection(id,newfd,p));
// 	ch->SetMessageCallback(std::bind(OnMessage,std::placeholders::_1,std::placeholders::_2));
// 	ch->SetServeCloseCallback(std::bind(OnConnextDis,std::placeholders::_1));
// 	ch->SetConnectCallback(std::bind(OnConnect,std::placeholders::_1));
// 	ch->EnableInactiveRelease(10);
// 	ch->Establish();
// 	_conn.insert(std::make_pair(id,ch));
// }

// int main()
// {
// 	srand(time(NULL));
// 	EventLoop loop;
// 	Socket s;
// 	s.CreateTcpServer(8080);
// 	Channel *channel = new Channel(&loop, s.Fd());
// 	channel->SetReadCallback(std::bind(Accept,&loop,channel));
// 	channel->EnableReadableEvent();
// 	while (true)
// 	{
// 		sleep(1);
// 		loop.Start();
// 	}
// 	s.Close();
// 	return 0;
// }

// #include "../source/Socket.hpp"
// #include "../source/Channel.hpp"
// #include "../source/Poller.hpp"
// #include "../source/EventLoop.hpp"
// #include "../source/Connection.hpp"
// #include "../source/Acceptor.hpp"

// std::unordered_map<int, Ptrconnection> _conn;
// int id = 0;
// EventLoop loop;

// void closecallback(Channel *channel)
// {
// 	DEL_LOG("close:%d", channel->Fd());
// 	channel->Remove();
// 	delete channel;
// }
// void readcallback(Channel *channel)
// {
// 	char buff[1024] = {0};
// 	int ret = ::recv(channel->Fd(), buff, sizeof(buff), 0);
// 	if (ret < 0)
// 	{
// 		closecallback(channel);
// 		return;
// 	}
// 	DEL_LOG("%s", buff);
// 	channel->EnableWriteableEvent();
// }
// void OnConnextDis(const Ptrconnection &con)
// {
// 	_conn.erase(con->Id());
// }
// void OnConnect(const Ptrconnection &con)
// {
// 	DEL_LOG("new connection : %p", con->GetContext());
// }

// void OnMessage(const Ptrconnection &con, Buffer *buff)
// {
// 	DEL_LOG("%s", buff->ReadPosition());
// 	buff->MoveReadOffset(buff->ReadableSize());
// 	std::string str = "hell bit";
// 	con->Send(str.c_str(), str.size());
// 	con->Shutdown();
// }

// void Accept(int newfd)
// {
// 	id++;
// 	Ptrconnection ch(new Connection(id, newfd, &loop));
// 	ch->SetMessageCallback(std::bind(OnMessage, std::placeholders::_1, std::placeholders::_2));
// 	ch->SetServeCloseCallback(std::bind(OnConnextDis, std::placeholders::_1));
// 	ch->SetConnectCallback(std::bind(OnConnect, std::placeholders::_1));
// 	ch->EnableInactiveRelease(10);
// 	ch->Establish();
// 	_conn.insert(std::make_pair(id, ch));
// }

// int main()
// {
// 	srand(time(NULL));
// 	Acceptor acceptor(&loop, 8080);
// 	acceptor.SetAcceptorCallback(std::bind(Accept, std::placeholders::_1));
// 	acceptor.Listen();
// 	while (true)
// 	{
// 		sleep(1);
// 		loop.Start();
// 	}
// 	return 0;
// }

#include "../source/Socket.hpp"
#include "../source/Channel.hpp"
#include "../source/Poller.hpp"
#include "../source/EventLoop.hpp"
#include "../source/Connection.hpp"
#include "../source/Acceptor.hpp"
#include "../source/LoopThread.hpp"
#include "../source/LoopThreadPool.hpp"

std::unordered_map<int, Ptrconnection> _conn;
int id = 0;
EventLoop loop;
LoopThreadPool *loop_loop;

void closecallback(Channel *channel)
{
	DEL_LOG("close:%d", channel->Fd());
	channel->Remove();
	delete channel;
}
void readcallback(Channel *channel)
{
	char buff[1024] = {0};
	int ret = ::recv(channel->Fd(), buff, sizeof(buff), 0);
	if (ret < 0)
	{
		closecallback(channel);
		return;
	}
	DEL_LOG("%s", buff);
	channel->EnableWriteableEvent();
}
void OnConnextDis(const Ptrconnection &con)
{
	_conn.erase(con->Id());
}
void OnConnect(const Ptrconnection &con)
{
	DEL_LOG("new connection : %p", con->GetContext());
}

void OnMessage(const Ptrconnection &con, Buffer *buff)
{
	DEL_LOG("%s", buff->ReadPosition());
	buff->MoveReadOffset(buff->ReadableSize());
	std::string str = "hell bit";
	con->Send(str.c_str(), str.size());
	con->Shutdown();
}

void Accept(int newfd)
{
	id++;
	Ptrconnection ch(new Connection(id, newfd, loop_loop->NextLoop()));
	ch->SetMessageCallback(std::bind(OnMessage, std::placeholders::_1, std::placeholders::_2));
	ch->SetServeCloseCallback(std::bind(OnConnextDis, std::placeholders::_1));
	ch->SetConnectCallback(std::bind(OnConnect, std::placeholders::_1));
	ch->EnableInactiveRelease(10);
	ch->Establish();
	_conn.insert(std::make_pair(id, ch));
	DEL_LOG("-------------");
}

int main()
{
	loop_loop = new LoopThreadPool(&loop);
	loop_loop->SetThreadCount(2);
	loop_loop->Create();
	Acceptor acceptor(&loop, 8080);
	acceptor.SetAcceptorCallback(std::bind(Accept, std::placeholders::_1));
	acceptor.Listen();
	loop.Start();
	return 0;
}